package com.voute.View.Activity;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.voute.App.App;
import com.voute.Base.BaseActivity;
import com.voute.Model.Bean.PersonalFeedBean;
import com.voute.Model.Bean.SearchResultEntity;
import com.voute.Presenter.IPersonalFeedPresenter;
import com.voute.Presenter.Impl.PersonalFeedPresenterImpl;
import com.voute.R;
import com.voute.View.Adapter.SearchResultAdapter;
import com.voute.View.IView.IPersonalFeedView;

import java.util.ArrayList;

/**
 * 我的创建
 * 我的收藏
 * 我的参与
 * 统一的Activity
 * Created by liucxu on 2017/1/20.
 */

public class PersonalInfoFeedsActivity extends BaseActivity implements IPersonalFeedView {

    private SearchResultAdapter adapter;
    private SwipeRefreshLayout swipe_refresh;
    private IPersonalFeedPresenter prensenter;
    private ArrayList<SearchResultEntity> datas = new ArrayList<>();
    private ListView content_list;
    private RelativeLayout back_layout;
    private TextView title_tv;
    private boolean stopLoadMore;
    private boolean isLoading;
    private boolean isRefresh = true;
    private String type = "feed_type_create";
    private String anchor = "0";
    private String user_id = "";
    private String user_name="";

    @Override
    public int getLayoutId() {
        return R.layout.activity_personal_feeds;
    }

    @Override
    public void setupView() {
        type = getIntent().getStringExtra("FEED_TYPE");
        if (TextUtils.isEmpty(type)) {
            this.finish();
        }
        user_id = getIntent().getStringExtra("key_user_id");
        if (TextUtils.isEmpty(user_id)) {
            user_id = "";
        }
        swipe_refresh = $(R.id.swipe_refresh);
        swipe_refresh.setColorSchemeColors(Color.parseColor("#0f88eb"));
        adapter = new SearchResultAdapter(this, datas);
        content_list = $(R.id.content_list);
        content_list.setAdapter(adapter);
        back_layout = $(R.id.back_layout);
        title_tv = $(R.id.title_tv);
        user_name=getIntent().getStringExtra("key_user_name");
        if(TextUtils.isEmpty(user_name)){
            if (type.equals("collected")) {
                title_tv.setText("我的收藏");
            } else if (type.equals("created")) {
                title_tv.setText("我的创建");
            } else {
                title_tv.setText("我的参与");
            }
        }
        else{
            if (type.equals("collected")) {
                title_tv.setText(user_name+"的收藏");
            } else if (type.equals("created")) {
                title_tv.setText(user_name+"的创建");
            } else {
                title_tv.setText(user_name+"的参与");
            }
        }
        back_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.GetInstance().finishActivity(PersonalInfoFeedsActivity.this);
            }
        });
        prensenter = new PersonalFeedPresenterImpl(this);
        swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                getPagePersonalFeeds(isRefresh);
            }
        });

        content_list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int last_visiable = absListView.getLastVisiblePosition();
                if (!stopLoadMore && !isLoading && (last_visiable > 0)) {
                    if (last_visiable + 2 > totalItemCount) {
                        isRefresh = false;
                        getPagePersonalFeeds(isRefresh);
                    }
                }
            }
        });
    }

    @Override
    public void setupData() {
        isRefresh = true;
        getPagePersonalFeeds(true);
    }

    @Override
    public void showPersonalFeeds(PersonalFeedBean beans) {
        if (beans != null) {
            if (beans.getStatus() == 0) {
                isLoading = false;
                if (isRefresh) {
                    anchor = "0";
                    datas.clear();
                    isRefresh = false;
                }
                if (beans.getData() != null) {
                    if (beans.getData() != null && beans.getData().getFeeds() != null && beans.getData().getFeeds().size() > 0) {
                        datas.addAll(beans.getData().getFeeds());
                        adapter.setDatas(datas);
                        stopLoadMore = false;
                    } else {
                        stopLoadMore = true;
                    }
                } else {
                    isLoading = false;
                    stopLoadMore = false;
                }
            } else {
                isLoading = false;
                stopLoadMore = false;
            }
        } else {
            isLoading = false;
            stopLoadMore = false;
        }
    }

    @Override
    public void showLoading() {
        if (isRefresh) {
            swipe_refresh.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        swipe_refresh.setRefreshing(false);
    }

    @Override
    public void showError(String errMsg) {
//        Snackbar.make(swipe_refresh, errMsg, Snackbar.LENGTH_SHORT).show();
        MaterialDialog notice_dialog = new MaterialDialog.Builder(PersonalInfoFeedsActivity.this).title("提示").positiveText("确认").cancelable(false).content(errMsg).build();
        notice_dialog.show();
    }

    private void getPagePersonalFeeds(boolean refresh) {
        String anchor = "0";
        if (refresh)
            anchor = "0";
        else {
            if (datas.size() == 0) {
                anchor = "0";
            } else {
                anchor = datas.get(datas.size() - 1).getId() + "";
            }
        }
        if (!refresh) {
            if (stopLoadMore) {
                return;
            } else {
                stopLoadMore = true;
                isLoading = true;
            }
        } else {
            isLoading = true;
            stopLoadMore = true;
        }
        prensenter.getPersonalFeed(type, anchor,user_id);
    }
}
